package whu.cn.whushare.ui.main.my;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.QuickContactBadge;
import android.widget.TextView;

import com.androidplus.util.StringUtil;
import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;
import com.handmark.pulltorefresh.library.internal.EmptyViewMethodAccessor;

import whu.cn.whushare.R;
import whu.cn.whushare.bean.Result;
import whu.cn.whushare.bean.User;
import whu.cn.whushare.common.UserManager;
import whu.cn.whushare.net.DownloadParams;
import whu.cn.whushare.net.Downloader;
import whu.cn.whushare.ui.common.BaseActivity;

/**
 * 文件名：ForgetPassActivity
 * 描述：
 * 作者：胡平平
 * 时间：2015/3/24
 */
@ViewMapping(id=R.layout.activity_forget_pass)              //获取“忘记密码”页面布局文件
public class ForgetPassActivity extends BaseActivity {
    @ViewMapping(id = R.id.email)                           //获取“Email”编辑框
    private EditText mEmail;

    private UserManager mUserManager;

    /*
     * 方法名：getStartIntent(Context context)
     * 功 能：获取跳转到当前activity的intent消息
     * 参 数：@param Context context-上下文信息
     * 返回值：@return 跳转到当前activity的intent
     */
    public static Intent getStartIntent(Context context) {
        Intent i = new Intent(context,ForgetPassActivity.class);
        return i;
    }

    @Override
    /**
     * 重写父类的方法，初始化当前activity
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initViewsAndActions();
    }

    //对动作和视图进行初始化
    private void initViewsAndActions() {
        View root = ViewMappingUtil.mapView(this, this);
        getTopBar(root);
        mTopBar.title.setText(R.string.find_pass);
        mTopBar.searchView.setVisibility(View.INVISIBLE);
        mTopBar.rightText.setVisibility(View.VISIBLE);
        mTopBar.rightText.setText(R.string.send_email);
        initAction();
    }

    //对动作进行初始化：具体指将忘记密码信息发送到有效的Email中
    private void initAction() {
        mUserManager = UserManager.getInstance(this);
        mTopBar.rightText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email=mEmail.getText().toString();
                if (validateEMail(email)) {
                    Downloader.getInstance(ForgetPassActivity.this).sendEmail(email,ForgetPassActivity.this);

                }
            }
        });

    }
    @Override
    /**
     * 重写父类的onSuccess方法，当密码成功找回时，提示用户
     */
    public <T> void onSuccess(DownloadParams.PARSING_TYPE type, Result<T> response, Object obj) {
        super.onSuccess(type, response, obj);

        switch (type) {
            case SEND_EMAIL:
                toast("密码已找回，请登录您的邮箱获取密码!");
                goBack();
                break;

            default:
                break;

        }
    }

    /**
     * 判断是否为有效Email
     * @param email
     * @return
     */
    private boolean validateEMail(String email) {
        String msg;
        msg = mUserManager.validateMail(email).getMsg();
        if (validateMsg(msg)) return false;
        return !validateMsg(msg);
    }

    /**
     * 判断是否是有效的邮件
     * @param msg
     * @return
     */
    private boolean validateMsg(String msg) {
        if (!StringUtil.isNullOrEmpty(msg)) {
            toast(msg);
            return true;
        }
        return false;
    }
}


